﻿var frame = new lg.frame({
    data: {
        nav:Vue.createApp({
            data() {
                return {
                    menu: [],
                    curr: null,
                    currSub: null,
                    selected: [],
                    SN: null
                }  
            },
            mounted: function () {
                //this.GetMenu();
                //this.GetLoginAdmin();
            },
            computed: {
                Menu: function () {
                    return this.menu.filter(p => p.FatherId == 0);
                },
                SubMenu: function () {
                    var that = this;
                    if (this.curr != null) {
                        return this.menu.filter(p => p.FatherId == that.curr.Id);
                    } else {
                        return null;
                    }
                },
            },
            methods: {
                GetMenu: function () {
                    var that = this;
                    lg.request({
                        url: "/base/GetModule",
                        success: function (d) {
                            if (d.code == 0) {
                                that.menu = d.data;
                               
                            }
                        }
                    })
                },
                GetSub: function (item) {
                    var sub = this.menu.filter(p => p.FatherId == item.Id);
                    if (sub && sub.length>0) {
                        return sub;
                    } else {
                        return null;
                    }
                },
                GetLoginAdmin: function () {
                    lg.request({
                        url: "/base/GetLoginAdmin",
                        success: res => {
                            if (res.code == 0) {
                                $("#UserName").text(res.data.Account);
                            }
                        }
                    })
                },
                GetLink: function (obj) {
                    if (obj.Link) {
                        return obj.Link;
                    } else {
                        var sub = this.menu.filter(p => p.FatherId == obj.Id && p.Link != '')[0];
                        if (sub) {
                            return sub.Link;
                        }
                    }
                },
                SetCurr: function (id, event) {
                    var arr = this.menu.filter(p => p.Id == id);
                    this.curr = arr[0];
                    $(".bodyWrap").addClass("open");
                    //$(event.target).parent().addClass("act").siblings("li").removeClass("act")
                },
                HasSub: function (id) {
                    var b = false;
                    var obj = this.menu.filter(p => p.FatherId == id);
                    if (obj != undefined && obj.length > 0) {
                        b = true;
                    }
                    return b;
                },
                SetCurrMenu: function (url, success) {
                    var that = this;
                    if (that.menu.length > 0) {
                        var sub = that.menu.filter(p =>p.Link&&p.Link.toLowerCase() == url.toLowerCase())[0];
                        if (sub) {
                            if (sub.FatherId == 0) {
                                that.curr = sub;
                            } else {
                                that.curr = that.menu.filter(p => p.Id == sub.FatherId)[0];
                                that.currSub = sub;
                                $(".bodyWrap").addClass("open");
                            }
                        }  
                    } else {
                        that.SN = sn;
                    }
                    if (typeof success == "function") {
                        success();
                    }
                },
                LogOut: function () {
                    lg.request({
                        url: "/base/LogOut",
                        success: res => {
                            if (res.code == 0) {
                                window.location.href = "/Lgm"
                            }
                        }
                    })
                }
            }
        }).mount('#navWrap')
    },
    mounted: function () {
        this.InitEvent();
        this.NavigateTo({ url: "/Lgm/welcome", title: "首页" })
    },
    methods: {
        ClearCache: function () {
            lg.request({
                url: "/base/ClearCache",
                success: res => {
                    if (res.code == 0) {
                        layer.msg("缓存已清除！", { icon: 1 })
                    }
                }
            })
        },
        LogOut: function () {
            lg.request({
                url: "/base/logOut",
                success: res => {
                    if (res.code == 0) {
                        window.location.href = "/Lgm";
                    } else {
                        layer.msg(res.msg, { icon: 2 })
                    }
                }
            })
        },
        ChangePassword: function () {
            layer.open({
                type: 2,
                title: '修改密码',
                shadeClose: true,
                shade: 0.8,
                area: ['500px', '320px'],
                content: '/Lgm/ChangePassword' //iframe的url
            });
        },
        ///全局属性注册
        InitEvent: function () {
            //查询事件
            layui.form.on('submit(bt_Search)', function (data) {
                if (data.elem.dataset.id) {
                    var arr = frame.pages.filter(p => p.id == data.elem.dataset.id);
                    if (arr.length > 0) {
                        var page = arr[0];
                        if (page.Search && typeof page.Search == "function") {
                            page.Search(data);
                        } else {
                            page.laytable.reload({
                                where: data.field,
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                }
                            })
                        }   
                    }
                } else {
                    console.error("缺少pageId参数");
                }
                return false;
            })
            $("body").on("click", ".tree-drop-down", function () {
                $(this).siblings(".lg-diy-tree-level").toggle();
                $(this).toggleClass("fa-plus-square fa-minus-square");
            });
            $("body").on("click", ".lg-diy-tree .layui-form-switch", function () {
                $(this).toggleClass("layui-form-onswitch");
            });
            $("body").on("click", ".layui-form-switch-all", function () {

            });
            $("body").on("click", ".tb-slide", function () {
                var id = $(this).parents("tr").data("id");
                var b = $(this).hasClass("fa-angle-down")
                if (b) {
                    $(this).removeClass("fa-angle-down").addClass("fa-angle-right");
                    $(".f-" + id).hide();
                } else {
                    $(this).removeClass("fa-angle-right").addClass("fa-angle-down");
                    $(".f-" + id).show();
                }
            })
        },
    },
    onPush: function (page) {
        console.log(page);
    },
    onDestroyed: function () {
        console.log("删除成功！")
    },
    onLink: function (event) {
        var url = $(event.currentTarget).attr("url");
        this.data.nav.SetCurrMenu(url);
    },
    methodType:"post"
});
var laySwitch = {
    render: function (n, e) {
        switch (n) {
            case 1:
                return "<div class='layui-unselect layui-form-switch layui-form-onswitch' lay-skin='_switch' lay-event='" + e + "'><em>ON</em><i></i></div>";
                break;
            default:
                return "<div class='layui-unselect layui-form-switch' lay-skin='_switch' lay-event='" + e + "'><em>OFF</em><i></i></div>";
        }
    },
    do: function (o) {
        if (!o.data) {
            o.data = {};
            o.data["id"] = o.obj.data.id;
            o.data[o.key] = o.obj.data[o.key] == 1 ? 0 : 1;
        }
        o.obj.data[o.key] = o.data[o.key];
        lg.request({
            url: o.url,
            method: "post",
            data: JSON.stringify(o.data),
            success: function (d) {
                if (d.code == 0) {
                    if (o.refresh && typeof o.refresh == "function") {
                        o.refresh();
                    } else {
                        o.obj.update(o.obj.data);
                    }
                    layer.msg("操作成功!", { icon: 1 });
                } else {
                    layer.msg(d.msg, { icon: 2 })
                }
            }
        });
    }
}

